package action;



import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.sql.Update;

import com.opensymphony.xwork2.ModelDriven;

import entity.Account;
import entity.Contest;
import service.AccountDAO;
import service.FollowDAO;
import service.impl.AccountDAOImpl;
import service.impl.FollowDAOImpl;


public class AccountAction extends SuperAction implements ModelDriven<Account>{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Account ac = new Account();

	public String login(){
		AccountDAO adao = new AccountDAOImpl();
		Account tempAc = adao.accountLogin(ac);
		if(tempAc != null)
		{
			//session锟叫憋拷锟斤拷锟矫伙拷锟斤拷录锟斤拷
			System.out.println(tempAc.getAccountName());
			session.setAttribute("loginAccount", tempAc);
			FollowDAO fdao = new FollowDAOImpl();
			System.out.println("tuxue2");
			List<Contest> conlist = fdao.query(tempAc.getAccountId());
			session.setAttribute("follow_Contest", conlist);
			System.out.println("tuxue");
			List<Contest> tempContests = (List<Contest>)session.getAttribute("follow_Contest");
			System.out.println(tempContests.get(0).toString() + "aaaaaaaaaaaaaaabbbbbbbbb");
			return "login_success";
			
		}
		else
		{
			System.out.println("login_failure");
			return "login_failure";
		}
	}
	
	@SkipValidation //璇ュ姩浣滄棤闇�獙璇�
	public String logout() {
		
		if(session.getAttribute("loginAccount")!=null)
		{
			session.removeAttribute("loginAccount");
		}
		return "logout_success";
	}
	
	public String register(){
		AccountDAO adao = new AccountDAOImpl();
		if(adao.accountRegister(this.ac) )
		{
			return "register_success";
		}
		else {
			return "register_failure";
		}
	}
	
	public String update(){
		Account	tempAc = (Account)session.getAttribute("loginAccount");
		ac.setAccountId(tempAc.getAccountId());
		
		AccountDAO adao = new AccountDAOImpl();
		System.out.println(ac.getAccountName());
		adao.update(ac);
		session.removeAttribute("loginAccount");
		session.setAttribute("loginAccount", ac);
		return "update_success";
	}

	@Override
	public Account getModel() {
		return this.ac;
	}
}
